// utils/util.js

export const utils = {
    classNames, isEmpty, isPhoneNumber, isEmail
}

/**
 * 构建css类名
 *
 * @example
 *
 *   classNames('foo', 'bar'); // => 'foo bar'
 *   classNames('foo', {bar: true}); // => 'foo bar'
 *   classNames({'foo-bar': true}); // => 'foo-bar'
 *   classNames({'foo-bar': false}); // => ''
 *   classNames({foo: true}, {bar: true}); // => 'foo bar'
 *   classNames({foo: true, bar: true}); // => 'foo bar'
 *
 * @param  {...any} args
 * @returns {string}
 */
function classNames(...args) {
    const classes = [];

    for (let i = 0; i < args.length; i += 1) {
        const arg = args[i];
        if (arg) {
            const argType = typeof arg;

            if (argType === 'string' || argType === 'number') {
                classes.push(arg);
            } else if (Array.isArray(arg)) {
                if (arg.length) {
                    const inner = classNames(...arg);
                    if (inner) {
                        classes.push(inner);
                    }
                }
            } else if (argType === 'object') {
                if (arg.toString !== Object.prototype.toString) {
                    classes.push(arg.toString());
                } else {
                    Object.keys(arg).forEach((key) => {
                        if (arg[key]) {
                            classes.push(key);
                        }
                    });
                }
            }
        }
    }

    return classes.join(' ');
}

function isEmpty(value) {
    return value === null || value === undefined || value === '';
}

function isPhoneNumber(value) {
    return /^1[3456789]\d{9}$/.test(value);
}

function isEmail(value) {
    return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(value);
}
